package org.hxzon.workeasy.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.validation.constraints.Size;

import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hxzon.workeasy.model.base.BelongToUser;

@Entity
public class EasyPost extends BelongToUser implements Comparable<EasyPost> {

    private static final long serialVersionUID = -2394146567804248902L;

    @OneToMany(mappedBy = "easyPost", fetch = FetchType.LAZY)
    private List<PostTag> postTags;

    @OneToMany(fetch = FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "ownerId", referencedColumnName = "id")
    private List<PostAttachment> attachFiles;

    @OneToMany(mappedBy = "easyPost", fetch = FetchType.LAZY)
    private List<PostReply> replys;

    @Column(columnDefinition = DefineVarchar64)
    @Size(max = DefineSize64)
    private String postTitle;

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
    private String postContent;

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
//  @Field(name="search")
    private String tagString;

    public String toString() {
        return this.postTitle + "::(" + this.postContent + ")";
    }

    public String print() {
        StringBuilder sb = new StringBuilder();
        sb.append("title:").append(postTitle).append(",author:").append(this.getUsername());
        return sb.toString();
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof EasyPost)) {
            return false;
        }
        EasyPost other = (EasyPost) o;
        return this.getId().equals(other.getId());
    }

    public int hashCode() {
        return this.getId().hashCode();
    }

    public int compareTo(EasyPost o) {
        return this.postTitle.compareTo(o.getPostTitle());
    }

    public List<PostTag> getPostTags() {
        return postTags;
    }

    public void setPostTags(List<PostTag> postTags) {
        this.postTags = postTags;
    }

    public List<PostAttachment> getAttachFiles() {
        return attachFiles;
    }

    public void setAttachFiles(List<PostAttachment> attachFiles) {
        this.attachFiles = attachFiles;
    }

    public List<PostReply> getReplys() {
        return replys;
    }

    public void setReplys(List<PostReply> replys) {
        this.replys = replys;
    }

    public String getPostTitle() {
        return postTitle;
    }

    public void setPostTitle(String postTitle) {
        this.postTitle = postTitle;
    }

    public String getPostContent() {
        return postContent;
    }

    public void setPostContent(String postContent) {
        this.postContent = postContent;
    }

    public String getTagString() {
        return tagString;
    }

    public void setTagString(String tagString) {
        this.tagString = tagString;
    }

}
